import { PrismaClient } from '@prisma/client';
import { hash } from 'bcryptjs';

const prisma = new PrismaClient();

interface TripData {
  title: string;
  description: string;
  startDate: Date;
  endDate: Date;
  imageUrl: string;
  activities: {
    title: string;
    date: Date;
    description: string;
  }[];
}

async function main() {
  // 创建一个管理员用户
  const hashedPassword = await hash('admin123', 10);
  const adminUser = await prisma.user.upsert({
    where: { username: 'admin' },
    update: {},
    create: {
      username: 'admin',
      password: hashedPassword,
      name: '管理员',
      email: 'admin@example.com',
    },
  });

  console.log('Created admin user:', adminUser);

  // 创建示例行程
  const trips: TripData[] = [
    {
      title: "云南七日游",
      description: "探索云南多元文化，体验少数民族风情",
      startDate: new Date("2024-08-01"),
      endDate: new Date("2024-08-07"),
      imageUrl: "/images/trips/yunnan.jpg",
      activities: [
        {
          title: "大理古城",
          date: new Date("2024-08-01T10:00:00"),
          description: "漫步大理古城，感受白族文化",
        },
        {
          title: "丽江古城",
          date: new Date("2024-08-03T09:00:00"),
          description: "探索世界文化遗产，纳西族古城",
        },
        {
          title: "泸沽湖",
          date: new Date("2024-08-05T08:00:00"),
          description: "摩梭人的母亲湖，感受独特的走婚文化",
        }
      ]
    },
    {
      title: "西藏朝圣之旅",
      description: "探访世界屋脊，体验藏传佛教文化",
      startDate: new Date("2024-09-15"),
      endDate: new Date("2024-09-21"),
      imageUrl: "/images/trips/tibet.jpg",
      activities: [
        {
          title: "布达拉宫",
          date: new Date("2024-09-15T09:00:00"),
          description: "参观世界文化遗产，了解藏传佛教历史",
        },
        {
          title: "大昭寺",
          date: new Date("2024-09-17T10:00:00"),
          description: "西藏最神圣的寺庙，感受信徒的虔诚",
        }
      ]
    }
  ];

  // 清除现有数据
  await prisma.activity.deleteMany();
  await prisma.trip.deleteMany();

  // 创建行程和活动
  for (const trip of trips) {
    await prisma.trip.create({
      data: {
        title: trip.title,
        description: trip.description,
        startDate: trip.startDate,
        endDate: trip.endDate,
        imageUrl: trip.imageUrl,
        isOfficial: true,
        isPublic: true,
        userId: adminUser.id,
        activities: {
          create: trip.activities
        }
      }
    });
  }

  console.log('示例数据创建成功！');
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  }); 